package com.csnf.servlet;

import com.csnf.entity.User;
import com.csnf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet({"/gteUser", "/editeUser", "/updateUser", "/deleteUser"})
public class UserServlet extends HttpServlet {
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        switch (servletPath) {
            case "/gteUser":
                getUserByNickName(req, resp);
                break;
            case "/editeUser":
                getUserById(req, resp);
                break;
            case "/deleteUser":
                deleteUser(req, resp);
                break;
        }
    }
    //添加查询用户的方法
    private void getUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_id = req.getParameter("user_id");
        User user = null;
        try {
            user = userService.getUserById(Integer.parseInt(user_id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("user", user);
        req.getRequestDispatcher("editeUser.jsp").forward(req, resp);
    }

    protected void getUserByNickName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nickname = req.getParameter("qeruyName");
        List<User> userList = null;
        try {
            userList = userService.getUserByNickName(nickname == null? "" : nickname);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("userList", userList);
        req.getRequestDispatcher("ok.jsp").forward(req, resp);
    }
    //添加修改用户的方法
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer user_id = Integer.valueOf((req.getParameter("user_id")));
        String user_name = req.getParameter("user_name");
        String user_nickname = req.getParameter("user_nickname");
        boolean flag = false;
        try {
            flag = userService.updateUser(new User(user_id, user_name, user_nickname));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (flag) {
            getUserByNickName(req, resp);
        } else {
            req.setAttribute("msg", "修改未成功");
            req.getRequestDispatcher("editeUser.jsp").forward(req, resp);
        }
    }

    // 添加的删除用户的方法
    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_id = req.getParameter("user_id");
        if (user_id!= null) {
            try {
                boolean deleteSuccess = userService.deleteUser(Integer.parseInt(user_id));
                if (deleteSuccess) {
                    resp.sendRedirect("gteUser?qeruyName=");
                } else {
                    req.setAttribute("msg", "删除用户失败");
                    req.getRequestDispatcher("ok.jsp").forward(req, resp);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (NumberFormatException e) {
                req.setAttribute("msg", "用户ID参数格式不正确");
                req.getRequestDispatcher("ok.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "未获取到有效的用户ID参数");
            req.getRequestDispatcher("ok.jsp").forward(req, resp);
        }
    }

    //添加的添加用户的方法
}